package Jets.animate
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class JAnimateBitmaps extends JAnimateBase
	{
		/**
		 * 位图数组
		 */
		public var bitmaps:Array;
		
		/**
		 * 是否在销毁的时候自动回收位图
		 */
		public var disposeWhenDestory:Boolean = true;
		
		private var _enableMouseEvent:Boolean;
		private var destoryed:Boolean;
		/**
		 * 进行位图缓存时，可以用它来控制缓存过程
		 */
		public var movieClipCacher:JMcToBitmaps
		/**
		 * 
		 * @param bitmaps	源位图数组
		 * @param labels	标签数组，内容为FrameLabel类型
		 * @param paused	是否暂停
		 * 
		 */
		public function JAnimateBitmaps(bitmaps:Array=null,labels:Array=null,paused:Boolean=false)
		{
			super(null,paused);
			if (!bitmaps)
				bitmaps = [];
			
			this.bitmaps = bitmaps;
			this.labels = labels;
			
			if (bitmaps && bitmaps.length > 0)
				setContent(bitmaps[0]);
			
			this.mouseEnabled = this.mouseChildren = false;
		}
		
		public override function set currentFrame(frame:int):void
		{
			if (frame < 1)
				frame = 1;
			if (frame > totalFrames)
				frame = totalFrames;
			
			if (super.currentFrame == frame)
				return;
			
			super.currentFrame = frame;
			
			setContent(bitmaps[frame - 1]);
			
		}
		
		public override function get totalFrames():int
		{
			return bitmaps.length;
		}
		
		/**
		 * 回收位图资源 
		 * 
		 */
		public function dispose():void
		{
			for each (var bitmapData:BitmapData in bitmaps)
			bitmapData.dispose();
		} 
		
		/** @inheritDoc*/
		protected override function onRelease(event:Event = null):void
		{
			if (destoryed)
				return;
			
			if (disposeWhenDestory)
				dispose();
			
			super.onRelease();
		}
		
		/**
		 * 从一个MovieClip生成
		 * 注意这个缓存是需要时间的，如果要在完全生成GBitmapMovieClip对象后进行一些操作，可监听GBitmapMovieClip的complete事件
		 * 
		 * @param mc	要转换的电影剪辑
		 * @param rect	绘制范围
		 * @param start	起始帧
		 * @param len	长度
		 * @param readWhenPlaying 是否在播放时顺便缓存
		 * @param limitTimeInFrame	每次缓存允许的最高时间
		 * @return 
		 * 
		 */
		public function createFromMovieClip(mc:MovieClip,rect:Point=null,point:Point=null,start:int = 1,len:int = -1):void
		{
			//mc:MovieClip,size:Point=null,offest:Point=null,start:int = 1,len:int = -1
			movieClipCacher = new JMcToBitmaps(mc,rect,point,start,len);
		
			
			movieClipCacher.addEventListener(Event.COMPLETE,cacherCompleteHandler);
		}
		
		
		private function cacherCompleteHandler(event:Event):void
		{
			movieClipCacher.removeEventListener(Event.COMPLETE,cacherCompleteHandler);
			
			this.bitmaps = movieClipCacher.result;
			this.labels = movieClipCacher.mc.currentLabels;
			this.dispatchEvent(new Event(Event.COMPLETE));
			
			movieClipCacher = null;
		}
		
		public function drawToBitmapData(target:BitmapData,offest:Point):void
		{
			var bitmapData:BitmapData = getContent().bitmapData;
			if (bitmapData)
				target.copyPixels(bitmapData,bitmapData.rect,offest,null,null,target.transparent);
		}
		
		
	}
}